<template>
	<view>
		<hx-navbar ref="hxnb" :config="config" @clickBtn="clickBtn">
			<view slot="bottom">
				<template>
					<view class="input-product">
						<u-search placeholder="搜索" v-model="searchData.keywords" @change="onSearchChange"
							:showAction="false"></u-search>
					</view>
				</template>
			</view>
		</hx-navbar>
		<u-loading-page :loading="pageLoading"></u-loading-page>

		<scroll-view class="" scroll-y refresher-enabled show-scrollbar :refresher-triggered="triggered"
			@refresherrefresh="$u.throttle(onRefresherrefresh, 200)" @scrolltolower='$u.throttle(onScrolltolower,1000)'
			:style="{height: `${screenHeight}px`, marginTop: '100rpx'}">

			<u-empty mode="list" v-if="listData.length === 0" :style="{height: `${screenHeight}px`}" />

			<!-- 列表 -->
			<view class="p-l-30 p-r-30">
				<view class="item" v-for="(item, index) in listData" :key="index" @tap="itemClick(item)">
					<view>
						<view class="name">{{item.code}} / {{item.name}}</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 筛选弹框 - 1 -->
		<u-popup :show="show" mode="right" overlayOpacity="0.3" @close="show = false">
			<view class="pop-view">
				<u-cell title="显示停用">
					<view slot="value">
						<u-switch v-model="searchData.displayDeactivation"></u-switch>
					</view>
				</u-cell>
				<view class="fot-btn flex flex-sw p-l-10 p-r-10">
					<u-button text="重置" shape="circle" class="reset m-r-10"></u-button>
					<u-button text="搜索" shape="circle"
						color="linear-gradient(to right, rgb(64, 158, 255), rgb(64, 158, 255), rgb(96, 180, 246), rgb(96, 180, 246))"></u-button>
				</view>
			</view>
		</u-popup>
		
		<!-- 新增单位 -->
		<u-modal :show="showUnit" title="新增单位" showCancelButton @cancel="showUnit = false">
			<view class="slot-content">
				<u--input class="input" placeholder="新增单位" border="surround"></u--input>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import _ from 'lodash'

	import {
		listOptionsPage
	} from '@/apis/bsd/customer.js'

	export default {
		data() {
			return {
				searchData: {
					keywords: '',
					displayDeactivation: false
				},
				screenHeight: 0,
				pageLoading: true,
				triggered: false,
				listData: [],
				pageInfo: {
					current: 1, // 当前页
					size: 10, // 每页数据条数
					sort: '', // 排序字段
					order: 'desc', // 排序方向
					total: 0 // 总数居量
				},
				config: {
					title: "选择计价单位",
					color: "#ffffff",
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [1, ["#409EFF", "#409EFF", "#60b4f6", "#60b4f6"]],
					rightButton: [{
							key: "btn1",
							icon: "&#xe727;",
							position: "left",
						},
						{
							key: "btn2",
							icon: "&#xe888;",
							position: "left",
						},
					],
				},
				show: false,
				showUnit: false
			}
		},
		onLoad() {
			this.initData()
		},
		onReady() {
			const sysInfo = uni.getSystemInfoSync()
			this.screenHeight = sysInfo.windowHeight - 100
		},
		methods: {
			initData() {
				this.listPage()
			},
			listPage() {
				const params = {
					page: this.pageInfo,
					data: this.searchData
				}
				uni.showLoading({
					title: "加载中"
				})
				listOptionsPage(params).then(res => {
					if (!res.data.flag) return
					const records = res.data.data.records || []
					this.listData.push(...records)
				}).finally(_ => {
					this.triggered = false
					this.pageLoading = false
					uni.hideLoading()
				})
			},
			onSearchChange() {
				this.handlerSearch()
			},
			handlerSearch: _.debounce(function() {
				this.listData = []
				this.listPage()
			}, 200),
			itemClick(row) {
				uni.$emit("on-selected-charge-unit", row)
				this.$Router.back()
			},
			onRefresherrefresh() {
				this.triggered = true
				this.listData = []
				this.listPage()
			},
			onScrolltolower() {},
			clickBtn(item) {
				if (item.key === "btn1") {
					this.showUnit = true
				}
				if (item.key === "btn2") {
					this.show = true
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		border-radius: 10rpx;
		background: #fff;
		padding: 30rpx;
		margin-top: 30rpx;

		.name {
			font-size: 40rpx;
		}
	}

	.input-product {
		width: 95%;
		margin: 20upx auto;
		border-radius: 50upx;
		height: 70upx;
		line-height: 70upx;
		color: #fff;
		display: flex;
	}

	.pop-view {
		width: 500rpx;
		padding-top: 190rpx;

		.fot-btn {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			padding-bottom: 20rpx;
		}

		.reset {
			color: #409eff;
			background-color: #e3edff;
		}
	}
</style>